<template>
  <div class="order container">
    <Top title="提交订单"></Top>
    <section>
      <div class="path">
        <h3 class="path-title">收货信息</h3>
        <div class="adddress" v-if="path.length == 0" @click="goPath">
          新增地址
        </div>
        <div
          class="path-content"
          v-for="(item, index) in path"
          :key="index"
          @click="goPath"
        >
          <div>
            <span>{{ item.name }}</span>
            <span>{{ item.tel }}</span>
          </div>
          <div>
            <span>{{ item.province }}</span>
            <span>{{ item.city }}</span>
            <span>{{ item.county }}</span>
            <span>{{ item.addressDetail }}</span>
          </div>
        </div>
      </div>
      <div class="payment">
        <div class="payment-title">支付方式：</div>
        <van-radio-group v-model="radioPayment">
          <van-radio name="wx">微信支付</van-radio>
          <van-radio name="ali">支付宝支付</van-radio>
        </van-radio-group>
      </div>
      <div class="goods" v-for="(item, index) in goodsList" :key="index">
        <ul>
          <li>
            <div>
              <img :src="item.goods_imgUrl" alt="" />
            </div>
            <div class="goods-content">
              <h4>{{ item.goods_name }}</h4>
              <div class="goods-total">
                <span>{{ item.goods_price }}</span>
                <span>x{{ item.goods_num }}</span>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </section>
    <footer>
      <div class="order-total">
        <!-- <span>共</span>
        <b>{{ carData.length }}</b>
        <span>件,</span> -->
        <span>总金额：</span>
        <em>¥{{ tatolPrice }}</em>
      </div>
      <div class="order-topay" @click="setOrder">提交订单</div>
    </footer>
  </div>
</template>
<script>
import Top from "@/views/login/top";
import { mapState, mapMutations, mapGetters } from "vuex";
import { getData } from "@/common/api/address";
import { submitOrder, payment } from "@/common/api/order";
import bus from "@/common/bus";
export default {
  components: {
    Top,
  },
  data() {
    return {
      radioPayment: "wx",
      path: [],
    };
  },
  computed: {
    ...mapGetters(["defaultPath"]),
    ...mapState({
      carData: (state) => state.cart.cartData,
      orderItem: (state) => state.order.orderItem,
      orderId: (state) => state.order.orderId,
    }),
    tatolPrice() {
      return this.$route.query.tatolPrice;
    },
    goodsId() {
      return JSON.parse(this.$route.query.id);
    },
    goodsList() {
      return this.goodsId.map((id) => {
        return this.carData.find((v) => v.id == id);
      });
    },
  },
  methods: {
    goPath() {
      this.$router.push({ name: "pathMessage", params: { type: "cart" } });
    },
    ...mapMutations(["getPathInfo"]),
    setOrder() {
      if (!this.path.length > 0) {
        return console.log("请添加收货地址");
      }
      

      let data = {
        orderId: this.orderId,
        name: '商品名称',
        price: this.tatolPrice,
      };

      // 发请求1.修改订单状态2.删除购物车数据
      submitOrder(this.orderId, this.goodsList).then((res) => {
        console.log(res);
        // 支付接口
        payment(data).then((r) => {
        
          if(r.data.data.code==200){           
              window.location.href=r.data.data.paymentUrl
          }
          
          
        });
      });
    },
  },
  mounted() {
    // 生成背景颜色
    document
      .querySelector("body")
      .setAttribute("style", "background-color:#f5f5f5");

    // 请求获取用户地址信息
    getData().then((res) => {
      //  vuex保存地址信息
      this.getPathInfo(res.data.data.data);

      if (this.defaultPath.length > 0) {
        this.path =
          this.orderItem.length > 0 ? this.orderItem : this.defaultPath;
      } else {
        if (res.data.data.data[0]) {
          this.path.push(res.data.data.data[0]);
        }
      }
    });
  },

  beforeDestroy() {
    //  清除背景色
    document.querySelector("body").removeAttribute("style");
  },
};
</script>

<style lang="scss" scoped>
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 1.173333rem;
  color: #fff;
  background-color: #b0352f;
  i {
    padding: 0 0.4rem;
    font-size: 0.586666rem;
  }
  span {
    font-weight: 300;
    font-size: 0.48rem;
  }
}
section {
  background-color: #f7f7f7;
  .path-title {
    padding: 0.4rem;
    font-size: 0.48rem;
  }
  .path-content {
    padding: 0.16rem 0.4rem;
    font-size: 0.373333rem;
    background-color: #ffffff;
    span {
      padding-right: 0.16rem;
    }
  }
  .payment {
    padding: 0.16rem 0.4rem;
    margin-top: 0.4rem;
    font-size: 0.426666rem;
    background-color: #ffffff;
    .van-radio-group {
      display: flex;
      padding: 0.16rem 0;
      .van-radio {
        padding-right: 0.266666rem;
      }
    }
  }
  .goods {
    padding: 0.16rem 0.4rem;
    margin-top: 0.4rem;
    font-size: 0.426666rem;
    background-color: #ffffff;
    ul {
      width: 100%;
      li {
        display: flex;
        width: 100%;
        img {
          width: 1.973333rem;
          height: 1.973333rem;
        }
        .goods-content {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          padding-left: 0.4rem;
          .goods-total {
            display: flex;
            justify-content: space-between;
          }
        }
      }
    }
  }
}
footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 1.2rem;
  border-top: 1px solid #ccc;
  .order-total {
    font-size: 0.426666rem;
    span {
      padding: 0 0.16rem;
    }
    b {
      color: #b0352f;
    }
    em {
      font-size: 0.48rem;
      color: #b0352f;
    }
  }
  .order-topay {
    width: 3.2rem;
    line-height: 1.2rem;
    color: #fff;
    font-size: 0.426666rem;
    text-align: center;
    background-color: #b0352f;
  }
}
</style>